class Solution {
public:
    int minimumRounds(vector<int>& tasks)
    {
        unordered_map<int, int> hash;
        for (auto& a : tasks)
            hash[a]++;
        int ans = 0;
        int n = tasks.size();
        for (int i = 0; i < n; i++)
        {
            if (hash.find(tasks[i]) != hash.end() && (hash[tasks[i]] >= 5 || hash[tasks[i]] == 3))
            {
                ans++;
                hash[tasks[i]] -= 3;
                if (hash[tasks[i]] == 0) hash.erase(tasks[i]);
            }
            else if (hash.find(tasks[i]) != hash.end() && hash[tasks[i]] < 5)
            {
                ans++;
                hash[tasks[i]] -= 2;
                if (hash[tasks[i]] == 0) hash.erase(tasks[i]);
            }
        }
        if (hash.size() != 0) return -1;
        return ans == 0 ? -1 : ans;
    }
};